using BasicData.Domain.Entities;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Produce.Application.Services
{
    /// <summary>
    /// 客户数据服务，用于获取BasicData模块中的客户数据
    /// </summary>
    public class CustomerDataService : ICustomerDataService
    {
        private readonly ISqlSugarClient _db;

        public CustomerDataService(ISqlSugarClient db)
        {
            _db = db;
        }

        /// <summary>
        /// 获取所有客户列表
        /// </summary>
        /// <returns>客户列表</returns>
        public List<CustomerModel> GetAllCustomers()
        {
            return _db.Queryable<CustomerModel>()
                .Where(x => x.IsDeleted == false)
                .ToList();
        }

        /// <summary>
        /// 根据ID获取客户
        /// </summary>
        /// <param name="id">客户ID</param>
        /// <returns>客户信息</returns>
        public CustomerModel GetCustomerById(long id)
        {
            return _db.Queryable<CustomerModel>()
                .Where(x => x.Id == id && x.IsDeleted == false)
                .First();
        }

        /// <summary>
        /// 根据姓名查找客户
        /// </summary>
        /// <param name="name">客户姓名</param>
        /// <returns>匹配的客户列表</returns>
        public List<CustomerModel> GetCustomersByName(string name)
        {
            if (string.IsNullOrEmpty(name))
                return new List<CustomerModel>();

            return _db.Queryable<CustomerModel>()
                .Where(x => x.CustomerName.Contains(name) && x.IsDeleted == false)
                .ToList();
        }
    }
}